package com.waitingresult.str;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Aspect
@Slf4j
@Component
public class TrimAspectj {
    @Pointcut("@annotation(com.waitingresult.str.Trim)")
    public void trimAspect() {
    }

    @Before("trimAspect()")
    public void aspect(JoinPoint point) throws IllegalAccessException {
        Object[] args = point.getArgs();
        for (Object arg : args) {
            Class<?> aClass = arg.getClass();
            Field[] fields = aClass.getDeclaredFields();
            for (Field field : fields) {
                if (field.isAnnotationPresent(Trim.class)) {
                    System.err.println(field);
                    field.setAccessible(true);
                    String o = (String) field.get(arg);
                    field.set(arg, o.trim());
                }
            }
        }
    }
}
